<?php

/**
 * SvcPHP
 *
 * Copyright (C) 2012 João Francisco Biondo Trinca
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * @package    SvcPHP
 * @author     João Francisco Biondo Trinca <wolfulus@gmail.com>
 * @copyright  2012 João Francisco Biondo Trinca <wolfulus@gmail.com>
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
 * @link       http://code.google.com/p/svcphp/
 *
 */

/**
 * Time plugin class
 */
class Svc_Template_Plugin_Gravatar implements Svc_Template_Plugin
{
    /**
     * Creates a new instance of the url plugin.
     */
    public function __construct()
    {
    }

    /**
     * Returns the plugin id.
     *
     * @return string
     */
    public function id()
    {
        return 'time';
    }

    /**
     * Installs the translator into the template engine.
     *
     * @param Svc_TemplateData $data
     */
    public function register(&$data)
    {
        $data->register('gravatar', function ($email, $s = 80, $d = 'mm', $r = 'g', $img = false, $attr = array())
        {
            return Svc_Template_Plugin_Gravatar::gravatar($email, $s, $d, $r, $img, $attr);
        });
    }

    /**
     * Get either a Gravatar URL or complete image tag for a specified email address.
     *
     * @param string $email The email address
     * @param int|string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
     * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
     * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
     * @param bool $img True to return a complete IMG tag False for just the URL
     * @param array $attr
     *
     * @return String containing either just a URL or a complete image tag
     * @source http://gravatar.com/site/implement/images/php/
     */
    public static function gravatar($email, $s = 80, $d = 'mm', $r = 'g', $img = false, $attr = array())
    {
        $url = 'http://www.gravatar.com/avatar/';
        $url .= strtolower(md5(strtolower(trim($email))));
        $url .= "?s=$s&d=$d&r=$r";
        if ($img)
        {
            $url = '<img src="' . $url . '"';
            foreach ($attr as $key => $val) $url .= ' ' . $key . '="' . $val . '"';
            $url .= ' />';
        }

        return $url;
    }
}
